﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevFx.Web.Code;
using DevFx.FrameWork;
using DevFx.Model;
using DevFx.FrameWork.Exception;

namespace DevFx.Web.Pages.Rights
{
    public partial class UserList : BasePage
    {
        #region 变量

        #endregion

        #region 属性
        /// <summary>
        /// 分页对象
        /// </summary>
        protected PagerCondition pager
        {
            get
            {
                if (ViewState["pager"] == null)
                    ViewState["pager"] = new PagerCondition();
                return ViewState["pager"] as PagerCondition;
            }
            set
            {
                ViewState["pager"] = value;
            }
        }
        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            Pager1.PageChangeEvent += new Web.Controls.Common.Pager.PageChange(PageChanged);
            if (!IsPostBack)
            {

                BindData();
            }
        }
        /// <summary>
        /// 分页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_Sorting(object sender, GridViewSortEventArgs e)
        {
            PushSort(e.SortExpression.ToString(), pager);
            BindData();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //修改
            if (e.CommandName == "editing")
            {
                //模态窗体关闭后刷新页面
                BindData();
            }
         
        }
        protected void btnadd_Click(object sender, EventArgs e)
        {
            //模态窗体关闭后刷新页面
            BindData();
        }
        /// <summary>
        /// 批量禁用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btndel_Click(object sender, EventArgs e)
        {
            int count = 0;
            try
            {
                using (Model.Entities db = new Entities())
                {
                    foreach (GridViewRow rows in this.gvList.Rows)
                    {
                        CheckBox cbx1 = rows.FindControl("cbx1") as CheckBox;
                        HiddenField hfID = rows.FindControl("hfID") as HiddenField;
                        if (cbx1.Checked)
                        {
                            Guid gid = new Guid(hfID.Value);
                            var m = db.SysUsers.FirstOrDefault(s => s.UserID == gid);
                            if (m != null)
                            {
                                m.Disabled = true;
                                //db.DeleteObject(m);
                                count += 1;
                            }
                        }
                    }
                    db.SaveChanges();
                    BindData();
                    MessageBox.Show(this.Page, "禁用成功，共禁用" + count + "条记录!");
                }
            }
            catch (BusinessException ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 批量禁用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEnabld_Click(object sender, EventArgs e)
        {
            int count = 0;
            try
            {
                using (Model.Entities db = new Entities())
                {
                    foreach (GridViewRow rows in this.gvList.Rows)
                    {
                        CheckBox cbx1 = rows.FindControl("cbx1") as CheckBox;
                        HiddenField hfID = rows.FindControl("hfID") as HiddenField;
                        if (cbx1.Checked)
                        {
                            Guid gid = new Guid(hfID.Value);
                            var m = db.SysUsers.FirstOrDefault(s => s.UserID == gid);
                            if (m != null)
                            {
                                m.Disabled = false;
                                //db.DeleteObject(m);
                                count += 1;
                            }
                        }
                    }
                    db.SaveChanges();
                    BindData();
                    MessageBox.Show(this.Page, "启用成功，共启用" + count + "条记录!");
                }
            }
            catch (BusinessException ex)
            {
                throw ex;
            }

        }
        protected void PageChanged(int CurPage)
        {
            pager.CurrentPage = CurPage;
            BindData();
        }
        #endregion

        #region 方法
        private void BindData()
        {
           // pager.IsPaging = false;
            pager.IsPaging = true;
            //判断总页数与当前页数
            if (pager.CurrentPage > pager.PageCount)
            {
                pager.CurrentPage = pager.PageCount;
            }
            if (pager.SortConditions.Count < 1)
            {
                SortCondition item = new SortCondition();
                item.SortDirection = FrameWork.SortDirection.ASC;
                item.SortField = "CreateDate";
                pager.SortConditions.Push(item);
            }
            List<SysUser> list = null;
            using (Model.Entities db = new Entities())
            {
                IQueryable<SysUser> EntityList = from c in db.SysUsers select c;
                EntityList = LinqHelper.SortingAndPaging(EntityList, pager);
                list = EntityList.ToList();
            }
            this.gvList.DataSource = list;
            this.gvList.DataBind();
            this.Pager1.BindData(pager);
        }
        #endregion

      

        
    }
}